package com.woniuxy.services.impl;
import com.woniuxy.daos.TbWorkerDao;
import com.woniuxy.entitys.TbWorker;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.daos.TbWorkerDao;
import com.woniuxy.entitys.TbWorker;
import com.woniuxy.entitys.TbRole;
import com.woniuxy.entitys.TbWorker;
import com.woniuxy.entitys.TbWorkerExample;
import com.woniuxy.services.WorkerService;
import com.woniuxy.utils.JWTUtils;
import com.woniuxy.utils.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

@Service
public class WorkerServiceImpl implements WorkerService {
    @Autowired
    private TbWorkerDao tbWorkerDao;
    @Autowired
    private JWTUtils jwtUtils;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<TbWorker> getFixWorkerList() {
        return tbWorkerDao.selFixWorkerList();
    }
    public PageInfo<TbWorker> getTbWorker(int pageNum, int pageSize, String uname,String phone,Integer rid) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbWorker> tbWorkers=tbWorkerDao.getTbWorker(uname,phone,rid);
        PageInfo<TbWorker> tbWorkerPageInfo = new PageInfo<>(tbWorkers);
        return tbWorkerPageInfo;
    }
    @Override
    public PageInfo<TbWorker> selAllWorkers(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbWorker> list =  tbWorkerDao.selectAllWorkers();
        PageInfo<TbWorker> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<TbWorker> selWokersByCondition(Integer pageSize, Integer pageNum, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbWorker> list =  tbWorkerDao.selByCondition(keyword);
        PageInfo<TbWorker> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int updateSoftdel(Integer wid) {
        int i = tbWorkerDao.updateSoftdel(wid);
        return i;
    }

    @Override
    public TbWorker loadWorkerInfo() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token  = servletRequestAttributes.getRequest().getHeader("token");
        String username = jwtUtils.getUsername(token);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        LoginUser loginUser = (LoginUser) valueOperations.get("user:"+username);
        Integer uid = loginUser.getUser().getUid();
        TbWorker worker = tbWorkerDao.loadWorkerInfo(uid);
        return worker;
    }

    @Override
    public int updWorkerInfo(TbWorker worker) {
        int count = tbWorkerDao.updWorkerInfoByUid(worker);
        return count ;
    }
}
